﻿using Publicuse.Interface;
using Publicuse.Util.Redis;
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Publicuse.Service
{
    public class RedisService : IRedisService
    {
        /// <summary>
        /// 插入数据到队列
        /// </summary>
        /// <param name="conIndex"></param>
        /// <param name="dbIndex"></param>
        /// <param name="msg"></param>
        /// <param name="key"></param>
        /// <param name="jsonData"></param>
        public void AddDataToQueue(int conIndex, int dbIndex, out string msg, string key, string jsonData)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.AddDataToQueue(key, jsonData);
            });

        }

        /// <summary>
        /// 插入对象到Hash
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="conIndex"></param>
        /// <param name="dbIndex"></param>
        /// <param name="msg"></param>
        /// <param name="key"></param>
        /// <param name="suit"></param>
        public void AddDbjectToHash<T>(int conIndex, int dbIndex, out string msg, string key, T suit) where T : new()
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.AddDbjectToHash(key, suit);
            });
        }

        /// <summary>
        /// 插入单个值到Hash
        /// </summary>
        /// <param name="conIndex"></param>
        /// <param name="dbIndex"></param>
        /// <param name="msg"></param>
        /// <param name="key"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public void AddHashValueByKey(int conIndex, int dbIndex, out string msg, string key, string name, string value)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.AddHashValueByKey(key, name, value);
            });
        }

        /// <summary>
        ///  如果hashid集合中存在key/value则不添加返回false，
        ///  如果不存在在添加key/value,返回true
        /// </summary>
        /// <param name="client"></param>
        /// <param name="hashid"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public bool AddHashValueByKeyNotExists(int conIndex, int dbIndex, out string msg, string hashid, string key, string value)
        {
            var result = true;
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                result = db.AddHashValueByKeyNotExists(hashid, key, value);
            });
            return result;
        }


        /// <summary>
        /// 修改Hash对应Key的Value加1
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public void AddHashValuesByKey(int conIndex, int dbIndex, out string msg, string key, Dictionary<string, string> keyValues)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.AddHashValuesByKey(key, keyValues);
            });
        }

        /// <summary>
        /// 修改Hash对应Key的Value加1
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public void AddHashValuesByKeyAdd(int conIndex, int dbIndex, out string msg, string key, string name, int value = 1)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.AddHashValuesByKeyAdd(key, name, value);
            });
        }

        /// <summary>
        /// 修改Hash对应Key的Value加1
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public void AddHashValuesByKeyAdd(int conIndex, int dbIndex, out string msg, string key, string name, long value = 1)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.AddHashValuesByKeyAdd(key, name, value);
            });
        }

        /// <summary>
        /// 插入数据到Sorted
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void AddSet(int conIndex, int dbIndex, out string msg, string key, string value)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.AddSet(key, value);
            });
        }

        /// <summary>
        /// 插入多条数据到Sorted
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="values"></param>
        public void AddSets(int conIndex, int dbIndex, out string msg, string key, List<string> values)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.AddSets(key, values);
            });
        }

        /// <summary>
        /// 插入数据到SortedSet
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="time"></param>
        public void AddSortedSet(int conIndex, int dbIndex, out string msg, string key, string value, long time)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.AddSortedSet(key, value, time);
            });
        }

        /// <summary>
        /// 插入数据到SortedSet
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="score"></param>
        public void AddSortedSet(int conIndex, int dbIndex, out string msg, string key, string value, double score)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.AddSortedSet(key, value, score);
            });
        }

        /// <summary>
        ///  插入数据到SortedSet
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>

        public void AddSortedSet(int conIndex, int dbIndex, out string msg, string key, string value)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.AddSortedSet(key, value);
            });
        }

        /// <summary>
        /// 插入多条数据到SortedSet
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="values"></param>
        /// <param name="time"></param>
        public void AddSortedSets(int conIndex, int dbIndex, out string msg, string key, List<string> values, double time)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.AddSortedSets(key, values, time);
            });
        }

        /// <summary>
        /// 插入单个数据到String
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void AddStringData(int conIndex, int dbIndex, out string msg, string key, string value)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.AddStringData(key, value);
            });
        }

        /// <summary>
        /// 插入多个数据到String
        /// </summary>
        /// <param name="client"></param>
        /// <param name="list"></param>
        public void AddStringDatas(int conIndex, int dbIndex, out string msg, Dictionary<string, string> list)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.AddStringDatas(list);
            });
        }

        /// <summary>
        /// 取消要发布的订阅
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="msg"></param>
        public void Cancelsubscription(int conIndex, int dbIndex, out string msg, string key)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.Cancelsubscription(key);
            });
        }

        /// <summary>
        /// 更换Redis索引库,默认是0号索引库
        /// </summary>
        /// <param name="client"></param>
        /// <param name="newDb"></param>
        /// <returns></returns>
        public IRedisClient ChangeDB(int conIndex, int dbIndex, out string msg, int newDb)
        {
            IRedisClient redisClient = null;
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                redisClient = db.ChangeDB(newDb);
            });
            return redisClient;
        }

        /// <summary>
        /// 提交事务捕获异常
        /// </summary>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public bool CommitTransac(int conIndex, int dbIndex, out string msg)
        {
            var result = true;
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                result = db.CreateTransaction().CommitTransac();
            });
            return result;
        }

        /// <summary>
        /// 移除某个Key数据
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        public void DeleteDataByKey(int conIndex, int dbIndex, out string msg, string key)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.DeleteDataByKey(key);
            });
        }

        /// <summary>
        /// 删除多个Key数据
        /// </summary>
        /// <param name="client"></param>
        /// <param name="keys"></param>
        public void DeleteDatasByKey(int conIndex, int dbIndex, out string msg, string[] keys)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.DeleteDatasByKey(keys);
            });
        }

        /// <summary>
        /// 移除Hash对应Key对应值
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="name"></param>
        public void DeleteHashValueByKey(int conIndex, int dbIndex, out string msg, string key, string name)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.DeleteHashValueByKey(key, name);
            });
        }

        /// <summary>
        /// 移除Set某个Key数据
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void DeleteSetByKey(int conIndex, int dbIndex, out string msg, string key, string value)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.DeleteHashValueByKey(key, value);
            });
        }

        /// <summary>
        /// 移除有序集合对应Key对应值
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="name"></param>
        public void DeleteSortedSetValueByKey(int conIndex, int dbIndex, out string msg, string key, string name)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.DeleteSortedSetValueByKey(key, name);
            });
        }

        /// <summary>
        /// 获取SortedSet所有信息
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public List<string> GetAllSortSetList(int conIndex, int dbIndex, out string msg, string key)
        {
            List<string> list = new List<string>();
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                list = db.GetAllSortSetList(key);
            });
            return list;
        }

        /// <summary>
        /// 获取SortedSet所有信息
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public List<string> GetAllSortSetList(int conIndex, int dbIndex, out string msg, string key, int start, int end, int sortType)
        {
            List<string> list = new List<string>();
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                list = db.GetAllSortSetList(key, start, end, sortType);
            });
            return list;
        }

        /// <summary>
        ///从队列获取数据
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetDataFromQueue(int conIndex, int dbIndex, out string msg, string key)
        {
            string result = string.Empty;
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                result = db.GetDataFromQueue(key);
            });
            return result;
        }

        /// <summary>
        /// 获取队列数据总数
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public long GetDataFromQueueLength(int conIndex, int dbIndex, out string msg, string key)
        {
            long result = 0;
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                result = db.GetDataFromQueueLength(key);
            });
            return result;
        }

        /// <summary>
        /// 获取队列里面所有的数据
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public List<string> GetDataFromQueueList(int conIndex, int dbIndex, out string msg, string key)
        {
            List<string> result = new List<string>();
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                result = db.GetDataFromQueueList(key);
            });
            return result;
        }

        /// <summary>
        /// 获取队列一条数据
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetDataFromQueueOne(int conIndex, int dbIndex, out string msg, string key)
        {
            string result = string.Empty;
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                result = db.GetDataFromQueueOne(key);
            });
            return result;
        }

        /// <summary>
        /// 获取Stirng的值
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetDataFromString(int conIndex, int dbIndex, out string msg, string key)
        {
            string result = string.Empty;
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                result = db.GetDataFromString(key);
            });
            return result;
        }

        /// <summary>
        /// 获取Hash某个Key的数量
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public long GetHashLengthByKey(int conIndex, int dbIndex, out string msg, string key)
        {
            long result = 0;
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                result = db.GetHashLengthByKey(key);
            });
            return result;
        }

        /// <summary>
        /// 获取Hash对应多个Key对应的值
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="names"></param>
        /// <returns></returns>
        public List<string> GetHashMoreByKeys(int conIndex, int dbIndex, out string msg, string key, string[] names)
        {
            List<string> result = new List<string>();
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                result = db.GetHashMoreByKeys(key, names);
            });
            return result;
        }

        /// <summary>
        /// 获取Hash所有的数据转换为字典集合
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public Dictionary<string, string> GetHashToDictionaryByKey(int conIndex, int dbIndex, out string msg, string key)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                result = db.GetHashToDictionaryByKey(key);
            });
            return result;
        }

        /// <summary>
        /// 获取Hash所有数据转换为实体对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public T GetHashToObjectByKey<T>(int conIndex, int dbIndex, out string msg, string key) where T : new()
        {
            T result = default(T);
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                result = db.GetHashToObjectByKey<T>(key);
            });
            return result;
        }

        /// <summary>
        ///获取Hash对应Key对应值
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public string GetHashValuByKey(int conIndex, int dbIndex, out string msg, string key, string name)
        {
            string result = string.Empty;
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                result = db.GetHashValuByKey(key, name);
            });
            return result;
        }

        /// <summary>
        /// 获取Hash包含特定字符的集合
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public Dictionary<string, string> GetHashValuLikeKey(int conIndex, int dbIndex, out string msg, string key, string value)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                result = db.GetHashValuLikeKey(key, value);
            });
            return result;
        }

        /// <summary>
        /// 获取所有的Set数据
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public List<string> GetSetAll(int conIndex, int dbIndex, out string msg, string key)
        {
            List<string> result = new List<string>();
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                result = db.GetSetAll(key);
            });
            return result;
        }

        /// <summary>
        ///  获取Set包含特定字符的集合
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public List<string> GetSetLikeKey(int conIndex, int dbIndex, out string msg, string key, string value)
        {
            List<string> result = new List<string>();
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                result = db.GetSetLikeKey(key, value);
            });
            return result;
        }

        /// <summary>
        ///  获取SortedSet包含特定字符的集合
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public Dictionary<string, double> GetSortSetLikeKey(int conIndex, int dbIndex, out string msg, string key, string value)
        {
            Dictionary<string, double> result = new Dictionary<string, double>();
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                result = db.GetSortSetLikeKey(key, value);
            });
            return result;
        }

        /// <summary>
        /// 判断Hash某个Key是否存在
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="name"></param>
        public bool HashExists(int conIndex, int dbIndex, out string msg, string key, string name)
        {
            bool result = true;
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                result = db.HashExists(key, name);
            });
            return result;
        }

        /// <summary>
        /// 判断某个Key信息是否存在
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool KeyExists(int conIndex, int dbIndex, out string msg, string key)
        {
            bool result = true;
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                result = db.KeyExists(key);
            });
            return result;
        }

        /// <summary>
        /// 发布订阅
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="message"></param>
        /// <param name="msg"></param>
        public void Publishsubscribe(int conIndex, int dbIndex, out string msg, string key, string message)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.Publishsubscribe(key, message);
            });
        }

        /// <summary>
        /// 判断Set某个Key是否存在
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool SetExists(int conIndex, int dbIndex, out string msg, string key, string name)
        {
            bool result = true;
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                result = db.SetExists(key, name);
            });
            return result;
        }

        /// <summary>
        /// 设置过期时间
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="timeSpan"></param>
        public void SetKeyExpire(int conIndex, int dbIndex, out string msg, string key, TimeSpan timeSpan)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.SetKeyExpire(key, timeSpan);
            });
        }

        /// <summary>
        /// 发布订阅
        /// </summary>
        /// <param name="client"></param>
        /// <param name="key"></param>
        /// <param name="message"></param>
        /// <param name="msg"></param>
        public void Subscribe(int conIndex, int dbIndex, out string msg, string key, Action<string, string, IRedisSubscription> actionOnMessage)
        {
            ServiceStackUtil.DoAction(conIndex, dbIndex, out msg, db =>
            {
                db.Subscribe(key, actionOnMessage);
            });
        }
    }
}
